/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.crossSection.CrossSection;
import br.ufmg.dees.insane.crossSection.RegCrossSection;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    CrossSectionDialog dgl = new CrossSectionDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since August 2004
 */
public final class CrossSectionDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private CardLayout cardManager = new CardLayout();
	private FemModel fmodel;
	
	private JPanel secButPane = new JPanel();
	private boolean secNewPressed = false;
	private String[] secLabelList;
	private JComboBox secListCombo = new JComboBox();
	private JButton secNewButton = new JButton();
	private JButton secModButton = new JButton();
	private JButton secOkButton = new JButton();
	private JButton secCancButton = new JButton();
	private JTextField secLabelField = new JTextField();
	private DecimalField secIxField;
	private DecimalField secIyField;
	private DecimalField secIzField;
	private DecimalField secAField;
	private DecimalField secHField;
	private DecimalField secThickField;
	public String secLabel;
	
	public boolean modified = false;
	private String svdLabel = "";
	private double svdIx = 0;
	private double svdIy = 0;
	private double svdIz = 0;
	private double svdArea = 0;
	private double svdHeight = 0;
	private double svdThickness = 0;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public CrossSectionDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		secNewButton.setText(parent.getResourceBundle().getString("gNewButton"));
		secModButton.setText(parent.getResourceBundle().getString("gModifyButton"));
		secOkButton.setText(parent.getResourceBundle().getString("gOkButton"));
		secCancButton.setText(parent.getResourceBundle().getString("gCancelButton"));
		
		setUpSectionsList();
		setUpSectionsCombo();
		
		setUpFormat();
		setUpFields();
		
		//Setting subpanel cross section
		JPanel sectionPane = new JPanel();
		sectionPane.setLayout(new BoxLayout(sectionPane, BoxLayout.Y_AXIS));
		sectionPane.setMaximumSize(new Dimension(550,155));
		sectionPane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dCSDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		secListCombo.setMaximumSize(new Dimension(150,26));
		secLabelField.setMaximumSize(new Dimension(150,26));
		secNewButton.setMaximumSize(new Dimension(90,26));
		secModButton.setMaximumSize(new Dimension(90,26));
		secOkButton.setMaximumSize(new Dimension(90,26));
		secCancButton.setMaximumSize(new Dimension(90,26));
		
		secListCombo.setMaximumRowCount(5);
		
		secIxField.setEditable(false);
		secIyField.setEditable(false);
		secIzField.setEditable(false);
		secAField.setEditable(false);
		secHField.setEditable(false);
		secThickField.setEditable(false);
		
		secListCombo.addItemListener(new SecComboListener());
		secNewButton.addActionListener(new SecNewListener());
		secModButton.addActionListener(new SecModListener());
		secOkButton.addActionListener(new SecOkListener());
		secCancButton.addActionListener(new SecCancListener());
		
		JPanel secBut1Pane = new JPanel();
		secBut1Pane.setLayout(new GridLayout(1,2,2,2));
		secBut1Pane.add(secListCombo);
		JPanel secBut1aPane = new JPanel();
		secBut1aPane.setLayout(new GridLayout(1,2,2,0));
		secBut1aPane.add(secNewButton);
		secBut1aPane.add(secModButton);
		secBut1Pane.add(secBut1aPane);
		
		JPanel secBut2Pane = new JPanel();
		secBut2Pane.setLayout(new GridLayout(1,2,2,2));
		secBut2Pane.add(secLabelField);
		JPanel secBut2aPane = new JPanel();
		secBut2aPane.setLayout(new GridLayout(1,2,2,0));
		secBut2aPane.add(secOkButton);
		secBut2aPane.add(secCancButton);
		secBut2Pane.add(secBut2aPane);
		
		secButPane.setLayout(cardManager);
		secButPane.add(secBut1Pane, "secCombo");
		secButPane.add(secBut2Pane, "secLabel");
		
		JPanel secPropPane = new JPanel();
		secPropPane.setLayout(new GridLayout(2,1,0,0));
		
		JPanel secPropSPane = new JPanel();
		secPropSPane.setLayout(new GridLayout(1,3,2,2));
		secPropSPane.setPreferredSize(new Dimension(500,26));
		JPanel secPropIPane = new JPanel();
		secPropIPane.setLayout(new GridLayout(1,3,2,2));
		secPropIPane.setPreferredSize(new Dimension(500,26));
		
		JPanel secIxPane = new JPanel();
		JPanel secIyPane = new JPanel();
		JPanel secIzPane = new JPanel();
		JPanel secAPane = new JPanel();
		JPanel secHPane = new JPanel();
		JPanel secThickPane = new JPanel();
		
		secIxPane.add(new JLabel("Ix:"));
		secIxPane.add(secIxField);
		secIyPane.add(new JLabel("Iy:"));
		secIyPane.add(secIyField);
		secIzPane.add(new JLabel("Iz:"));
		secIzPane.add(secIzField);
		secAPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog02")));
		secAPane.add(secAField);
		secHPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog03")));
		secHPane.add(secHField);
		secThickPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog11")));
		secThickPane.add(secThickField);
		
		secPropSPane.add(secIxPane);
		secPropSPane.add(secIyPane);
		secPropSPane.add(secIzPane);
		secPropIPane.add(secAPane);
		secPropIPane.add(secHPane);
		secPropIPane.add(secThickPane);
		
		secPropPane.add(secPropSPane);
		secPropPane.add(secPropIPane);
		
		sectionPane.add(secButPane);
		sectionPane.add(secPropPane);
		
		setPane(sectionPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dCSDialog04"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			
			int secIndex = secListCombo.getSelectedIndex();
			if (secIndex>0)
				secLabel = ((CrossSection)fmodel.getSectionsList().get(secIndex-1)).getLabel();
			else secLabel = null;
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			Element elm = (Element)this.parent.getController().getSelection().get(0);
			
			String str;
			int num;
			
			//Setting cross section fields
			if (elm.getCrossSection() != null)
				str = elm.getCrossSection().getLabel();
			else str = "";
			num = fmodel.getNumberOfSections();
			int secIndex = 0;
			for (int i=0; i<num; i++) {
				if (str.equals(secLabelList[i+1]))
					secIndex = i+1;
			}
			secListCombo.setSelectedIndex(secIndex);
			try {
				CrossSection cs = elm.getCrossSection();
				secIxField = new DecimalField(cs.getIx(), 7, numberFormat);
				secIyField = new DecimalField(cs.getIy(), 7, numberFormat);
				secIzField = new DecimalField(cs.getIz(), 7, numberFormat);
				secAField = new DecimalField(cs.getArea(), 7, numberFormat);
				secHField = new DecimalField(cs.getHeight(), 7, numberFormat);
				secThickField = new DecimalField(cs.getThickness(), 7, numberFormat);
			} catch (Exception e) {
				secIxField = new DecimalField(0, 7, numberFormat);
				secIyField = new DecimalField(0, 7, numberFormat);
				secIzField = new DecimalField(0, 7, numberFormat);
				secAField = new DecimalField(0, 7, numberFormat);
				secHField = new DecimalField(0, 7, numberFormat);
				secThickField = new DecimalField(0, 7, numberFormat);
				secIxField.setText(" ");
				secIyField.setText(" ");
				secIzField.setText(" ");
				secAField.setText(" ");
				secHField.setText(" ");
				secThickField.setText(" ");
				secModButton.setEnabled(false);
			}
			
		} else {
			
			//Setting cross section fields
			secIxField = new DecimalField(0, 7, numberFormat);
			secIyField = new DecimalField(0, 7, numberFormat);
			secIzField = new DecimalField(0, 7, numberFormat);
			secAField = new DecimalField(0, 7, numberFormat);
			secHField = new DecimalField(0, 7, numberFormat);
			secThickField = new DecimalField(0, 7, numberFormat);
			secListCombo.setSelectedIndex(0);
			secIxField.setText(" ");
			secIyField.setText(" ");
			secIzField.setText(" ");
			secAField.setText(" ");
			secHField.setText(" ");
			secThickField.setText(" ");
			secModButton.setEnabled(false);
			
		}
		
	}
	
//*****************************************************************************
	
	public void setUpSectionsList() {
		
		int num = fmodel.getNumberOfSections();
		
		if (num > 0) {
			secLabelList = new String[num+1];
			secLabelList[0] = "--- " + parent.getResourceBundle().getString("dCSDialog05") + " ---";
			for (int i=0; i<num; i++)
				secLabelList[i+1] = ((CrossSection)(parent.getModel().getDiscreteModel()).getSectionsList().get(i)).getLabel();
		}else {
			secLabelList = new String[1];
			secLabelList[0] = "----- " + parent.getResourceBundle().getString("dCSDialog06") + " -----";
		}
		
	}
	
//*****************************************************************************
	
	public void setUpSectionsCombo() {
		
		secListCombo.removeAllItems();
		for (int i=0; i<secLabelList.length; i++)
			secListCombo.addItem(secLabelList[i]);
		
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecComboListener implements ItemListener {
		public void itemStateChanged(ItemEvent evt) {
			
			int index = secListCombo.getSelectedIndex();
			
			if (index > 0 ) {
				
				CrossSection section = (CrossSection)fmodel.getSectionsList().get(index-1);
				
				secIxField.setValue(section.getIx());
				secIyField.setValue(section.getIy());
				secIzField.setValue(section.getIz());
				secAField.setValue(section.getArea());
				secHField.setValue(section.getHeight());
				secThickField.setValue(section.getThickness());
				
				secModButton.setEnabled(true);
			
			} else {
				
				secIxField.setText(" ");
				secIyField.setText(" ");
				secIzField.setText(" ");
				secAField.setText(" ");
				secHField.setText(" ");
				secThickField.setText(" ");
				
				secModButton.setEnabled(false);
				
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecNewListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(secButPane, "secLabel");
			setButtonsEnabled(false);
			
			svdLabel = "";
			svdIx = 0;
			svdIy = 0;
			svdIz = 0;
			svdArea = 0;
			svdHeight = 0;
			svdThickness = 0;
			
			secIxField.setEditable(true);
			secIyField.setEditable(true);
			secIzField.setEditable(true);
			secAField.setEditable(true);
			secHField.setEditable(true);
			secThickField.setEditable(true);
			
			int index = fmodel.getNumberOfSections() +1;
			secLabelField.setText(parent.getResourceBundle().getString("dCSDialog07") + " " + index);
			secIxField.setValue(0);
			secIyField.setValue(0);
			secIzField.setValue(0);
			secAField.setValue(0);
			secHField.setValue(0);
			secThickField.setValue(0);
			
			secNewPressed = true;
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecModListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(secButPane, "secLabel");
			setButtonsEnabled(false);
			
			svdLabel = secLabelField.getText();
			svdIx = secIxField.getValue();
			svdIy = secIyField.getValue();
			svdIz = secIzField.getValue();
			svdArea = secAField.getValue();
			svdHeight = secHField.getValue();
			svdThickness = secThickField.getValue();
			
			secIxField.setEditable(true);
			secIyField.setEditable(true);
			secIzField.setEditable(true);
			secAField.setEditable(true);
			secHField.setEditable(true);
			secThickField.setEditable(true);
			secLabelField.setText(secLabelList[secListCombo.getSelectedIndex()]);
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecOkListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			boolean existentLabel = false;
			for (int i=0; i<secLabelList.length; i++) {
				if (secLabelList[i].equals(secLabelField.getText()))
					existentLabel=true;
			}

			if (secLabelField.getText().equals(" ") || secLabelField.getText().equals("")) {
				JOptionPane.showMessageDialog(parent,
					parent.getResourceBundle().getString("dCSDialog08"),
					parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
			
			} else if (existentLabel && secNewPressed) {
				JOptionPane.showMessageDialog(parent,
					parent.getResourceBundle().getString("dCSDialog09"),
					parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
				
			} else {
				
				double ix = secIxField.getValue();
				double iy = secIyField.getValue();
				double iz = secIzField.getValue();
				double a = secAField.getValue();
				double h = secHField.getValue();
				double t = secThickField.getValue();
				
				if (ix<0 || iy<0 || iz<0 || a<0 || h<0 || t<0) {
					
					JOptionPane.showMessageDialog(parent,
						parent.getResourceBundle().getString("dCSDialog10"),
						parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
					
					if (ix<0) {
						secIxField.setValue(0);
						secIxField.setText(parent.getResourceBundle().getString("gmInvalidData"));
					}
					if (iy<0) {
						secIyField.setValue(0);
						secIyField.setText(parent.getResourceBundle().getString("gmInvalidData"));
					}
					if (iz<0) {
						secIzField.setValue(0);
						secIzField.setText(parent.getResourceBundle().getString("gmInvalidData"));
					}
					if (a<0) {
						secAField.setValue(0);
						secAField.setText(parent.getResourceBundle().getString("gmInvalidData"));
					}
					if (h<0) {
						secHField.setValue(0);
						secHField.setText(parent.getResourceBundle().getString("gmInvalidData"));
					}
					
				} else {
					
					cardManager.show(secButPane, "secCombo");
					setButtonsEnabled(true);
					
					secIxField.setEditable(false);
					secIyField.setEditable(false);
					secIzField.setEditable(false);
					secAField.setEditable(false);
					secHField.setEditable(false);
					secThickField.setEditable(false);
					
					if (!svdLabel.equals(secLabelField.getText()))
						modified = true;
					if (svdIx != secIxField.getValue())
						modified = true;
					if (svdIy != secIyField.getValue())
						modified = true;
					if (svdIz != secIzField.getValue())
						modified = true;
					if (svdArea != secAField.getValue())
						modified = true;
					if (svdHeight != secHField.getValue())
						modified = true;
					if (svdThickness != secThickField.getValue())
						modified = true;
					
					CrossSection section = new RegCrossSection();
					
					if (secNewPressed) {
						section.setLabel(secLabelField.getText());
						section.setIx(ix);
						section.setIy(iy);
						section.setIz(iz);
						section.setArea(a);
						section.setHeight(h);
						section.setThickness(t);
						fmodel.add(section);
						secNewPressed = false;
					
					} else {
						int index = secListCombo.getSelectedIndex();
						section = (CrossSection)fmodel.getSectionsList().get(index-1);
						section.setLabel(secLabelField.getText());
						section.setIx(ix);
						section.setIy(iy);
						section.setIz(iz);
						section.setArea(a);
						section.setHeight(h);
						section.setThickness(t);
					}
					
					setUpSectionsList();
					setUpSectionsCombo();
					
					int j=0;
					for (int i=0; i<secLabelList.length; i++) {
						if (secLabelList[i].equals(section.getLabel()))
							j=i;
					}
					
					secListCombo.setSelectedIndex(j);
					
				}
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecCancListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(secButPane, "secCombo");
			setButtonsEnabled(true);
			
			secIxField.setEditable(false);
			secIyField.setEditable(false);
			secIzField.setEditable(false);
			secAField.setEditable(false);
			secHField.setEditable(false);
			secThickField.setEditable(false);
			
			int index = secListCombo.getSelectedIndex();
			
			if (index==0) {
				secIxField.setText("");
				secIyField.setText("");
				secIzField.setText("");
				secAField.setText("");
				secHField.setText("");
				secThickField.setText("");
				
			} else {
				CrossSection section = (CrossSection)fmodel.getSectionsList().get(index-1);
				
				secIxField.setValue(section.getIx());
				secIyField.setValue(section.getIy());
				secIzField.setValue(section.getIz());
				secAField.setValue(section.getArea());
				secHField.setValue(section.getHeight());
				secThickField.setValue(section.getThickness());
				
			}
			
			secNewPressed = false;
			
		}
	}
	
//*****************************************************************************
}

